home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1998 September
/
Macworld (1998-09).dmg
/
Shareware World
/
Info
/
For Developers
/
Script Tools 1.3.6
/
Examples
/
Regular Expression Example II
< prev
next >
Wrap
Text File
|
1993-07-08
|
3KB
|
100 lines
--
-- Get the user to locate the params.test file in the Examples directory
--
set paramFile to choose file with prompt "Select params.good or params.bad" of type "TEXT"
set refNum to open file paramFile for reading
--
-- Compile the pattern strings we're going to look for
--
set comment to compile regular expression ".*#.*"
set param1 to compile regular expression "Default Width:[ ]*([0-9]*)"
set param2 to compile regular expression "Default Height:[ ]*([0-9]*)"
set param3 to compile regular expression "Username:[ ]*([A-Za-z0-9]*)"
set param4 to compile regular expression "Username:[ ]*\"(.*)\""
--
-- Populate parameter variables with default values
--
set defWidth to 0
set defHeight to 0
set defUsername to "unknown"
--
-- Read through the file looking for parameters
--
set paramMatched to true
try
repeat while paramMatched
--
-- Read in the next line
--
set input to read file refNum
--
-- If the line is not a comment line or a blank line then check to see
-- which parameter is being set
--
if not matched of (match regular expression comment to input) and length of input ≠ 0 then
set paramMatched to false
--
-- Default width?
--
set result to match regular expression param1 to input
if (matched of result) then
set defWidth to match 1 of result
set paramMatched to true
end if
--
-- Default height?
--
set result to match regular expression param2 to input
if (matched of result) then
set paramMatched to true
set defHeight to match 1 of result
end if
--
-- First username form?
--
set result to match regular expression param3 to input
if (matched of result) then
set paramMatched to true
set defUsername to match 1 of result
end if
--
-- Second username form?
--
set result to match regular expression param4 to input
if (matched of result) then
set paramMatched to true
set defUsername to match 1 of result
end if
end if
end repeat
--
-- If we get here it means the repear look existed because of a bad parameter. Otherwise
-- the error trap woould have cought the end of file.
--
display dialog "Bad parameter file line: “" & input & "”" ¬
buttons {"Cancel"} ¬
default button "Cancel"
close file refNum
on error
--
-- Assume EOF
--
close file refNum
end try
--
-- Show the parameters as they are now
--
if paramMatched then ¬
display dialog "Def Width: " & defWidth & ", Def. Height: " & defHeight & ", Username: “" & defUsername & "”" ¬
buttons {"OK"} default button "OK"